﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Net.Mail;

namespace MsgQueueTest
{
    public class MailManager
    {
        public static void SendMail()
        {
            var appConfigHelper = new AppSettingsReader();
            var account = (string)appConfigHelper.GetValue("EmailAccout", typeof(string));
            var pass = (string)appConfigHelper.GetValue("EmailPass", typeof(string));

            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(TrustAllCertificateValidationCallback);
            var smtpClient = new SmtpClient("smtp.gmail.com")
                                 {
                                     Credentials = new NetworkCredential(account, pass),
                                     Port = 587,
                                     EnableSsl = true
                                 };


            var mailMessage = new MailMessage(new MailAddress("zhougenhai@gmail.com", "lance"), new MailAddress("lance.zhou.suryani@gmail.com", "lance"))
                                  {
                                      Subject = "Test C#",
                                      Body = "Test ......ing .....ing ....."
                                  };

            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Send(mailMessage);

        }

        private static bool TrustAllCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslpolicyerrors)
        {
            return true;
        }
    }
}
